<template>
  <div id="addQuestionView">
    <h2>发贴</h2>
    <a-form :model="form" label-align="left">
      <a-form-item field="title" label="标题">
        <a-input v-model="form.title" placeholder="请输入标题" />
      </a-form-item>
      <a-form-item field="tags" label="标签">
        <a-input-tag
          v-model="form.tags"
          placeholder="请选择标签(点击回车添加标签)"
          allow-clear
        />
      </a-form-item>
      <a-form-item field="content" label="帖子内容">
        <MdEditor :value="form.content" :handle-change="onContentChange" />
      </a-form-item>
      <div style="margin-top: 16px" />
      <a-form-item>
        <a-button type="primary" style="min-width: 200px" @click="doSubmit"
          >提交
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import MdEditor from "@/components/MdEditor.vue";
import { PostControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { ref } from "vue";

let form = ref({
  content: "",
  tags: [],
  title: "",
});

/**
 *     content?: string;
 *     tags?: Array<string>;
 *     title?: string;
 */
const doSubmit = async () => {
  // 添加帖子
  const res = await PostControllerService.addPostUsingPost(form.value);
  if (res.code === 0) {
    message.success("帖子发布成功");
  } else {
    message.error("帖子发布失败，" + res.message);
  }
};

const onContentChange = (value: string) => {
  form.value.content = value;
};
</script>

<style scoped></style>
